<template>
  <div class="filiale-admin-list table-theme">
    <head-title title="合作--分公司管理员"></head-title>
    <el-card>
      <div class="top-menu">
        <div class="menu-left">
            <select-view
              :option-list="options"
              :value="type"
              placeholder="角色类型"
              type="type"
              @value="setSelectValue"
            >
            </select-view>
            <select-view
              :option-list="orderByList"
              :value="order_by"
              placeholder="排序"
              type="order_by"
              @value="setSelectValue"
            >
            </select-view>
            <select-view
              :option-list="ascList"
              :value="asc"
              placeholder="排序"
              type="asc"
              @value="setSelectValue"
            >
            </select-view>
        </div>
        <div class="menu-right">
          <refresh-button @refresh="refreshPage"></refresh-button>
          <search-view
            :option="searchOption"
            @value="goSearch"
            @clear="refreshPage"
          ></search-view>
          <div class="add" style="margin-right: 10px">
            <el-button type="primary" @click="add">添加</el-button>
          </div>
        </div>
      </div>
      <p class="data-number">总数据数：{{ total }}</p>
      <el-table :data="wList">
        <el-table-column
          label="序号"
          align="center"
          prop="filiale_admin_id"
        ></el-table-column>
        <el-table-column
          label="子公司ID"
          align="center"
          prop="filiale_id"
        ></el-table-column>
        <el-table-column
          label="用户ID"
          align="center"
          prop="user_id"
        ></el-table-column>
        <el-table-column label="类型" align="center" prop="">
          <template slot-scope="scope">
            <p>{{ options[scope.row.type].label }}</p>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="160">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="edit(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              @click="delAdmin(scope.row.filiale_admin_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pages">
        <el-pagination
          background
          layout="prev, pager, next, jumper"
          :total="total"
          @current-change="init"
          :current-page="page"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import addFilialeAdmin from "@/components/layer-dialog/filiale/add-filiale-admin.vue";
import SelectView from '@/components/SelectView';
export default {
	components:{
		SelectView
	},
  props: {
    filiale_id: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      selectContent: "", // 分类搜索平输入框的值
      classSelect: "", // 分类搜索
      searchOption: [
        {
          value: 0,
          label: "子公司ID",
        },
        {
          value: 1,
          label: "管理员ID",
        },
        {
          value: 2,
          label: "用户ID",
        },
      ],
      options: [
        {
          value: 0,
          label: "所有角色",
        },
        {
          value: 1,
          label: "超级管理员",
        },
        {
          value: 2,
          label: "财务",
        },
        {
          value: 3,
          label: "业务员",
        },
      ],
      order_by: "add_time",
      orderByList: [
        {
          value: "add_time",
          label: "时间排序",
        },
        {
          value: "sort",
          label: "序号排序",
        },
      ],
      asc: 2,
      ascList: [
        {
          value: 1,
          label: "升序",
        },
        {
          value: 2,
          label: "降序",
        },
      ],
      type: 0,
      wList: [],
      total: 0,
      totalData: 0,
      page: 0,
    };
  },
  methods: {
    // 初始化列表
    init(e) {
      this.page = e;
      let data = {
        page: e - 1 ? e - 1 : 0,
        limit: 10,
        type: this.type == 0 ? "" : this.type,
        order_by: this.order_by,
        asc: this.asc,
      };
      if (this.filiale_id != 0) {
        data.filiale_id = this.filiale_id;
      }

      if (this.classSelect == 0 && this.selectContent != "")
        data.filiale_id = this.selectContent;
      if (this.classSelect == 1 && this.selectContent != "")
        data.filiale_admin_id = this.selectContent;
      if (this.classSelect == 2 && this.selectContent != "")
        data.user_id = this.selectContent;

      this.$http(this.$ApiList.FilialeAdminList, data).then((res) => {
        this.wList = res.datas;
        this.total = res.count;
      });
    },
    goSearch(classSelect, selectContent) {
      this.classSelect = classSelect;
      this.selectContent = selectContent;
      this.init();
    },
    // 刷新
    refreshPage() {
      this.classSelect = "";
      this.selectContent = "";
      this.page = 1;
      this.init();
    },
    // 上下架
    upDown(e, filiale_id) {
      if (filiale_id == 1) {
        this.$layer.msg("不能下架总部");
        return;
      }
      let data = {
        filiale_id: filiale_id,
        status: e,
      };
      this.$http(this.$ApiList.filialeOut, data).then((res) => {
        this.$layer.msg(res.datas);
        this.init(this.page);
      });
    },
    add() {
      this.$layer.iframe({
        title: "添加管理员",
        maxmin: true,
        area: ["450px", "380px"],
        content: {
          content: addFilialeAdmin, //传递的组件对象
          parent: this, //当前的vue对象
          data: {
            type: 0,
          }, //props
        },
      });
    },
    delAdmin(filiale_admin_id) {
      this.$confirm("确定删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            filiale_admin_id: filiale_admin_id,
          };
          this.$http(this.$ApiList.FilialeAdminOut, data).then((res) => {
            this.$layer.msg(res.datas);
            this.init(this.page);
          });
        })
        .catch(() => {});
    },
    edit(item) {
      this.$layer.iframe({
        title: "编辑管理员",
        maxmin: true,
        area: ["450px", "380px"],
        content: {
          content: addFilialeAdmin, //传递的组件对象
          parent: this, //当前的vue对象
          data: {
            type: 1,
            info: item,
          }, //props
        },
      });
    },
	setSelectValue(obj){
		const { type,value } = obj
		switch (type) {
			case 'type':
				this.type = value
				break;
			case 'order_by':
				this.order_by = value
				break;
			case 'asc':
				this.asc = value
				break;
			default:
				break;
		}
		this.init()
	}
  },
  created() {
    this.init();
  },
};
</script>
<style lang="scss">
.filiale-admin-list {
  padding: 10px;
  width: 100%;
}
</style>
